import html
import json
import re
import sys
import urllib.parse

import requests


def getM3U8Urls(src: str, desc: str):
	rfile = open(src, encoding='utf-8')
	wfile = open(desc, 'a', encoding='utf-8')
	for line in rfile:
		contents = line.strip().split('|')
		name, url = contents
		ret = requests.get(url=url)
		rtxt = ret.text
		pattern = re.compile('player_data=(\{.+?})<')
		ctx = re.findall(pattern, rtxt)
		res = json.loads(ctx[0])
		# url_raw = base64.urlsafe_b64decode(res['url'])
		url_raw = unescape(res['url'])
		command = f'ffget {url_raw} --no-log --save-dir f:\\temp --save-name {name} \n'
		wfile.write(command)
		print(name, url_raw)


def unescape(string):
	string = urllib.parse.unquote(string)
	quoted = html.unescape(string).encode(sys.getfilesystemencoding()).decode('utf-8')
	# 转成中文
	return re.sub(r'%u([a-fA-F0-9]{4}|[a-fA-F0-9]{2})', lambda m: chr(int(m.group(1), 16)), quoted)


if __name__ == '__main__':
	getM3U8Urls('../resources/frxxz.txt', '../resources/frxxz.m3u8')
